`
https://leetcode.cn/problems/search-in-rotated-sorted-array-ii/
`

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {boolean}
 */
var search = function (nums, target) {
  // 基于 33. 搜索旋转排序数组 的写法
  // https://leetcode.cn/problems/search-in-rotated-sorted-array/

  const check = (i) => {
    const x = nums[i]
    if (x > nums[right]) {
      return target > nums[right] && x >= target
    }
    return target > nums[right] || x >= target
  }

  let left = -1, right = nums.length - 1
  while (left + 1 < right) {
    const mid = left + Math.floor((right - left) / 2)
    if (nums[mid] === nums[right]) {
      right--
    } else if (check(mid)) {
      right = mid
    } else {
      left = mid
    }
  }
  return nums[right] === target
};